<template>
  <div>
    <el-row>
      <el-col>
        <el-row :gutter="20">
          <el-col
            :span="4"
            v-for="(item, index) in list"
            :key="index"
            class="elcolcla"
            ><router-link
              :to="{
                path: '/details',
                query: { productID: item.goods_id },
              }"
            >
              <div class="grid-content">
                <div class="g_img">
                  <img
                    :src="$target + '/sharePath/' + item.pics[0].pic"
                    alt=""
                  />
                </div>
                <span class="g_title">{{ item.goods_name }}</span
                ><br />
                <span class="g_gold">￥{{ item.goods_price }}</span>
              </div>
            </router-link></el-col
          >
        </el-row>
      </el-col>
    </el-row>
    <!-- 分页栏 -->
    <el-row>
      <el-col :span="20" :offset="2" class="uil">
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page.sync="currentPage1"
          layout="total, prev, pager, next, jumper"
          :total="total"
          :page-size="24"
          background
        >
        </el-pagination>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  name: "",
  data() {
    return {
      currentPage1: 1, //初始默认页码
      currentList: "", //当前商品列表
      list: [],
      total: 1,
    };
  },
  components: {},
  mounted() {},
  methods: {
    // 刷新页面数据
    async changePage(index) {
      let catId = 0;
      if (
        this.$route.query !== undefined ||
        this.$route.query !== null ||
        this.$route.query !== {}
      ) {
        catId = this.$route.query.catId;
      }
      const loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0)",
      });
      if (catId !== "0") {
        const { data: res } = await this.$axios.get(
          `/goods?pagesize=24&pagenum=${index}&cateOneId=` + catId,
          {}
        );
        this.total = res.data.total;
        this.list = res.data.goods;
        loading.close();
      } else {
        const { data: res } = await this.$axios.get(
          `/goods?pagesize=24&pagenum=${index}`,
          {}
        );
        this.total = res.data.total;
        this.list = res.data.goods;
        loading.close();
      }
    },
    handleCurrentChange(val) {
      this.changePage(val);
      console.log(`当前页: ${val}`);
    },
  },
  created() {
    this.changePage(1);
    console.log(this.$route.query);
  },
};
</script>

<style scoped>
a {
  text-decoration: none;
  color: #000;
}

.grid-content {
  border-radius: 4px;
  display: flex;
  max-height: 322px;
  flex-direction: column;
  align-items: center;
  background: white;
  box-sizing: border-box;
  margin-top: 20px;
  text-align: center;
}
.g_img {
  box-sizing: border-box;
}
.g_img img {
  width: 80%;
  height: 140px;
  margin: 20px 0px;
}
.g_title {
  font-size: 15px;
  width: 80%;
  height: 40px;
  line-height: 40px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.g_gold {
  height: 20px;
  line-height: 20px;
  margin-bottom: 5px;
}
.elcolcla {
  border-radius: 4px;
  -webkit-transition: all 0.2s linear;
  transition: all 0.2s linear;
  z-index: 1;
}
.elcolcla:hover {
  z-index: 2;
  -webkit-box-shadow: 0 15px 30px rgba(0, 0, 0, 0.1);
  box-shadow: 0 15px 30px rgba(0, 0, 0, 0.1);
  -webkit-transform: translate3d(0, -2px, 0);
  transform: translate3d(0, -2px, 0);
}
.uil {
  height: 60px;
  margin-top: 20px;
  text-align: left;
}
</style>